package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.system.domain.SysHouseDecorationMoment;
import com.ruoyi.project.system.domain.SysMoment;
import com.ruoyi.project.system.service.ISysHouseDecorationMomentService;
import com.ruoyi.project.system.service.ISysMomentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysHouseDecoration;
import com.ruoyi.project.system.service.ISysHouseDecorationService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 整屋装修Controller
 *
 * @author jjp
 * @date 2020-04-26
 */
@RestController
@Api("整屋装修")
@RequestMapping("/system/decoration")
public class SysHouseDecorationController extends BaseController {
    @Autowired
    private ISysHouseDecorationService sysHouseDecorationService;
    @Autowired
    private ISysHouseDecorationMomentService sysHouseDecorationMomentService;
    @Autowired
    private ISysMomentService sysMomentService;


    /**
     * 查询整屋装修列表
     */
    @PreAuthorize("@ss.hasPermi('system:decoration:list')")
    @ApiOperation("查询整屋装修列表")
    @GetMapping("/list")
    public TableDataInfo list(SysHouseDecoration sysHouseDecoration) {
        startPage();
        List<SysHouseDecoration> list = sysHouseDecorationService.selectSysHouseDecorationList(sysHouseDecoration);
        List<Long> sysMomentList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Long decorationId = list.get(i).getDecorationId();
            SysHouseDecorationMoment sysHouseDecorationMoment = new SysHouseDecorationMoment();
            sysHouseDecorationMoment.setDecorationId(decorationId);
            List<SysHouseDecorationMoment> sysHouseDecorationMomentList = sysHouseDecorationMomentService.selectSysHouseDecorationMomentList(sysHouseDecorationMoment);
            for (int j = 0; j < sysHouseDecorationMomentList.size(); j++) {
                sysMomentList.add(sysHouseDecorationMomentList.get(j).getMomentId());
            }
            list.get(i).setMomentId(sysMomentList);
        }
        return getDataTable(list);
    }

    /**
     * 获取整屋装修详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:decoration:query')")
    @ApiOperation("获取整屋装修详细信息")
    @GetMapping(value = "/{decorationId}")
    public AjaxResult getInfo(@PathVariable("decorationId") Long decorationId) {
        SysHouseDecoration sysHouseDecoration = sysHouseDecorationService.selectSysHouseDecorationById(decorationId);
        List<Long> sysMomentList = new ArrayList<>();
        SysHouseDecorationMoment sysHouseDecorationMoment = new SysHouseDecorationMoment();
        sysHouseDecorationMoment.setDecorationId(decorationId);
        List<SysHouseDecorationMoment> sysHouseDecorationMomentList = sysHouseDecorationMomentService.selectSysHouseDecorationMomentList(sysHouseDecorationMoment);
        for (int j = 0; j < sysHouseDecorationMomentList.size(); j++) {
            Long momentId = sysHouseDecorationMomentList.get(j).getMomentId();
            sysMomentList.add(momentId);
        }
        sysHouseDecoration.setMomentId(sysMomentList);
        return AjaxResult.success(sysHouseDecoration);
    }

    /**
     * 新增整屋装修
     */
    @PreAuthorize("@ss.hasPermi('system:decoration:add')")
    @ApiOperation("新增整屋装修")
    @Log(title = "整屋装修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHouseDecoration sysHouseDecoration) {
        sysHouseDecoration.setDelFlag("0");
        sysHouseDecorationService.insertSysHouseDecoration(sysHouseDecoration);
        List<Long> momentId = sysHouseDecoration.getMomentId();
        if (momentId == null) {
            return AjaxResult.error("数据不对！");
        }
        for (int i = 0; i < momentId.size(); i++) {
            SysHouseDecorationMoment sysHouseDecorationMoment = new SysHouseDecorationMoment();
            sysHouseDecorationMoment.setDecorationId(sysHouseDecoration.getDecorationId());
            sysHouseDecorationMoment.setMomentId(momentId.get(i));
            sysHouseDecorationMomentService.insertSysHouseDecorationMoment(sysHouseDecorationMoment);
        }
        return AjaxResult.success();
    }

    /**
     * 修改整屋装修
     */
    @PreAuthorize("@ss.hasPermi('system:decoration:edit')")
    @ApiOperation("修改整屋装修")
    @Log(title = "整屋装修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHouseDecoration sysHouseDecoration) {
        List<Long> momentId = sysHouseDecoration.getMomentId();
        SysHouseDecorationMoment sysHouseDecorationMoment = new SysHouseDecorationMoment();
        Long decorationId = sysHouseDecoration.getDecorationId();
        sysHouseDecorationMomentService.deleteSysHouseDecorationMomentByDecorationId(decorationId);
        for (int i = 0; i < momentId.size(); i++) {
            sysHouseDecorationMoment.setDecorationId(decorationId);
            sysHouseDecorationMoment.setMomentId(momentId.get(i));
            sysHouseDecorationMomentService.insertSysHouseDecorationMoment(sysHouseDecorationMoment);
        }
        return toAjax(sysHouseDecorationService.updateSysHouseDecoration(sysHouseDecoration));
    }

    /**
     * 删除整屋装修
     */
    @PreAuthorize("@ss.hasPermi('system:decoration:remove')")
    @ApiOperation("删除整屋装修")
    @Log(title = "整屋装修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{decorationId}")
    public AjaxResult remove(@PathVariable Long decorationId) {
        SysHouseDecoration sysHouseDecoration = new SysHouseDecoration();
        sysHouseDecoration.setDelFlag("1");
        sysHouseDecoration.setDecorationId(decorationId);
        return toAjax(sysHouseDecorationService.updateSysHouseDecoration(sysHouseDecoration));
    }

    /**
     * 查询平台精彩瞬间列表
     */
    @PreAuthorize("@ss.hasPermi('system:moment:list')")
    @ApiOperation("查看所有精彩瞬间")
    @GetMapping("/momentList")
    public AjaxResult list(SysMoment sysMoment) {
        List<SysMoment> list = sysMomentService.selectSysMomentList(sysMoment);
        return AjaxResult.success(list);
    }
}
